//
//  FocusonViewController.m
//  WE
//
//  Created by 要帅气的生活 on 15/11/20.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "FocusonViewController.h"
#import "Size.h"
#import "MyAttentionTableViewCell.h"
#import "MyAttentionView.h"
#import "MyHospitalCollectionView.h"
#import "RegistrationViewController.h"
#import "ScanViewController.h"
#import "NumberSearchViewController.h"
#import "HospitalHomepageViewController.h"
@interface FocusonViewController ()<UIScrollViewAccessibilityDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UISegmentedControl *segmentControl;
@property (nonatomic,strong) UIScrollView *scroll;
@property (nonatomic,strong) UIPageControl *page;
@end


@implementation FocusonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的关注";
    self.view.backgroundColor = [UIColor whiteColor];
    [self scrollView];
    [self segment];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    
}

-(void)scrollView{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, WindowWidth, WindowHeight-64)];
    [self.view addSubview:_scroll];
    //内容视图大小
    self.scroll.contentSize = CGSizeMake(WindowWidth*2, 0);
    //滚动  默认YES
    self.scroll.scrollEnabled = NO;
    self.scroll.showsHorizontalScrollIndicator = YES;
    
    for (int i = 0; i<2; i++) {
        UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(i*WindowWidth, 0, WindowWidth, _scroll.frame.size.height)];
        [_scroll addSubview:table];
        table.rowHeight = 50;
        table.delegate = self;
        table.dataSource = self;
        table.tag = 360+i;
    }
    [self addview];
}
-(void)segment{
    self.segmentControl  = [[UISegmentedControl alloc] initWithItems:@[@"我的医生",@"我的医院"]];
    _segmentControl.frame = CGRectMake(margin_30 * Width, 84 * Height, WindowWidth - margin_30 * Width*2, margin_34);
    _segmentControl.selectedSegmentIndex = 0;
    [_segmentControl setTintColor:[UIColor blueColor]];
    [_segmentControl addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segmentControl];
}
- (void)doClick:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        [self.scroll setContentOffset:CGPointMake( 0 ,-64) animated:YES];
    }else if (segment.selectedSegmentIndex == 1){
        [self.scroll setContentOffset:CGPointMake(WindowWidth ,-64) animated:YES];
    }
}

-(void)addview{
    MyAttentionView *myview = [[MyAttentionView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 180*Height)];
    myview.label.text = @"您还没有关注过任何医生";
    myview.dsya.text = @"您可以通过以下方式查找医生:";
    ((UITableView *)[self.view viewWithTag:360]).tableHeaderView = myview;
    
    UIView *myViewTwo = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight-100)];
    MyAttentionView *myviewtow = [[MyAttentionView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 180*Height)];
    [myViewTwo addSubview:myviewtow];
    myviewtow.label.text = @"您还没有关注过任何医院";
    myviewtow.dsya.text = @"推荐您关注以下医院";
    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 180*Height, WindowWidth, myViewTwo.frame.size.height/2-20*Height)];
    [myViewTwo addSubview:scrollview];
    //内容视图大小
    scrollview.contentSize = CGSizeMake(3*WindowWidth, 0);
    //滚动  默认YES
    scrollview.scrollEnabled = YES;
    //显示整张照片 默认NO
    scrollview.pagingEnabled = YES;
    //边境反弹效果 默认YES
    scrollview.bounces = YES;
    //显示或隐藏 下面指示条  默认开启
    scrollview.showsHorizontalScrollIndicator = NO;
    scrollview.tag = 366;
    scrollview.delegate =self;
    _page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, myViewTwo.frame.size.height/2 + 160*Height+5, WindowWidth, 20)];
    [myViewTwo addSubview:_page];
    _page.numberOfPages = 3;
    _page.pageIndicatorTintColor = [UIColor grayColor];
    _page.currentPageIndicatorTintColor = [UIColor blackColor];
    //[page addTarget:self action:@selector(<#page:#>) forControlEvents:UIControlEventValueChanged];
    
    for (int i = 0 ; i < 3; i++) {
        for (int j = 0 ; j < 3; j++) {
            MyHospitalCollectionView *myhosview = [[MyHospitalCollectionView alloc] initWithFrame:CGRectMake(i*WindowWidth, j*scrollview.frame.size.height/3, WindowWidth, scrollview.frame.size.height/3)];
            [scrollview addSubview:myhosview];
            myhosview.button.tag = 1100+i*10+j;
            [myhosview.button addTarget:self action:@selector(hospital:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    UIButton *attention  = [UIButton buttonWithType:UIButtonTypeSystem];
    attention.frame = CGRectMake(20 , myViewTwo.frame.size.height/2 + 160*Height+25 , WindowWidth-40, 40);
    //设置按钮上的文字
    [attention setTitle:@"关注" forState:UIControlStateNormal];
    attention.tintColor =  [UIColor whiteColor];
    attention.backgroundColor = [UIColor blueColor];
    //设置按钮点击事件
    [attention addTarget:self action:@selector(attention) forControlEvents:UIControlEventTouchUpInside];
    [myViewTwo addSubview:attention];
    
    ((UITableView *)[self.view viewWithTag:361]).tableHeaderView = myViewTwo;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 360) {
        return 3;
    }else{
        return 0;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = @[@"按医院科室查找",@"扫一扫找医生",@"按手机号码找医生"];
    if (tableView.tag == 360) {
        MyAttentionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"my"];
        if (cell == nil) {
            cell = [[MyAttentionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"my"];
        }
        cell.label.text = arr[indexPath.row];
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mykong"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mykong"];
        }
        return cell;
    }
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag == 366) {
        
        self.page.currentPage = scrollView.contentOffset.x / WindowWidth;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath.row) {
        [self.navigationController pushViewController:[[RegistrationViewController alloc] init] animated:YES];
        
    } else if(indexPath.row == 1){
        [self.navigationController pushViewController:[[ScanViewController alloc] init] animated:YES];
    }else {
        [self.navigationController pushViewController:[[NumberSearchViewController alloc] init] animated:YES];
    }
    
    
    
}

-(void)attention
{
    NSLog(@"关注");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)hospital:(UIButton *)button
{
    NSLog(@"医院 %ld",button.tag);
    [self.navigationController pushViewController:[[HospitalHomepageViewController alloc] init] animated:YES];
}

@end
